package com.stlm2.controller;

import com.stlm2.dems.service.KeFuService;
import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by apple on 2017.12.14.
 */
@Controller
@RequestMapping("/keFuC")
public class KeFuController {

    @Resource
    private KeFuService keFuService;


    @RequestMapping(value="/listView",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/keFu/list");
        return modelAndView;
    }


    @RequestMapping(value="/addView",produces="application/json;charset=UTF-8")
    public ModelAndView addView(ModelAndView modelAndView){
        modelAndView.setViewName("/keFu/add");
        return modelAndView;
    }


    @RequestMapping(value="/detailsView",produces="application/json;charset=UTF-8")
    public ModelAndView detailsView(ModelAndView modelAndView,String id){
        modelAndView.setViewName("/keFu/details");
        modelAndView.addObject("id",id);
        return modelAndView;
    }


    /**
     *   添加
     *   ffl
     * @param model
     * @param token
     * @param weChatNumber
     * @param weChatCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/add.json",produces="application/json;charset=UTF-8")
    public  String add(ModelMap model, String token,  String weChatNumber,String weChatCode){
        String data=null;
        try {
            data=keFuService.addKeFu(token, weChatNumber, weChatCode);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"添加失败",data);
        }
        return  data;
    }


    /**
     * 编辑
     * ffl
     * @param token
     * @param id
     * @param weChatNumber
     * @param weChatCode
     * @param state
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/edit.json",produces="application/json;charset=UTF-8")
    public String editKeFu(String token,String id,String weChatNumber,String weChatCode,Boolean state){
        String data=null;
        try {
            data=keFuService.editKeFu(token, id, weChatNumber, weChatCode, state);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"编辑失败",data);
        }
        return  data;
    }


    /**
     * 后台列表页
     * ffl
     * 2017-12-14 11:17:30
     * @param model
     * @param page
     * @param token
     * @param weChatNumber
     * @param state
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/list.json",produces="application/json;charset=UTF-8")
    public String getCrmList(ModelMap model, String page,String token,String weChatNumber,Boolean state) {

        String data = null;
        try {
            data = keFuService.getCrmList(model, page, token, weChatNumber, state);
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0, "查询失败", data);
        }
        return data;
    }


    /**
     *  获取详情
     * ffl
     * 2017-12-14 12:05:34
     * @param id
     * @param token
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getCrmDetails.json",produces="application/json;charset=UTF-8")
    public String getCrmDetails(String id,String token){
        String data = null;
        try {
            data = keFuService.getCrmDetails(id, token);
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0, "查询失败", data);
        }
        return data;
    }

    /**
     *  修改人工客服状态 是否有效
     * ffl
     * 2017-12-15 11:21:04
     * @param token
     * @param id
     * @param state
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/updateState.json",produces="application/json;charset=UTF-8")
    public String editKeFuState(String token,String id,Boolean state){
        String data = null;
        try {
            data = keFuService.editKeFuState(token, id, state);
        } catch (Exception e) {
            e.printStackTrace();
            return CodeContent.getModelByCode(0, "操作失败", data);
        }
        return data;
    }
}
